package dflow.application.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import dflow.domain.*;
import dflow.domain.Dto.UserWithBlogs;
import dflow.domain.mapper.TemplateMapper;
import dflow.repository.BlogRepository;
import dflow.repository.UserRepository;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Queue;

/**
 * Created by dustin on 2017/7/9.
 */
@Service
public class BlogService extends BaseService {
    @Autowired
    BlogRepository blogRepository;

    @Autowired
    UserRepository userRepository;

    @Autowired
    TemplateMapper templateMapper;

    public PageTemplateDetail pageTemplateDetails(long id){
        //分页处理，显示第一页的10条数据
        PageTemplateDetail res=templateMapper.getTemplateDetail(id);
        return  res;
    }
    /**
     * @Description: mybatis分页查询测试
     * @param:   * @param null
     * @Author: dustin
     * @Date: 2017-12-11 16:43
     */
    public PageInfo<PageTemplateDetail> pageTemplateDetailsList(int pageName, int pageSize, long id, int nameLength){
        //分页处理，显示第一页的10条数据
        PageHelper.startPage(1, 10);
        Page<PageTemplateDetail> res=templateMapper.getTemplateDetails(id,nameLength);
        PageInfo<PageTemplateDetail> pageInfo = new PageInfo<>(res);
        return  pageInfo;
    }

    public List<PageModule> getPageModule(int pageIndex){
        QPageModule qPageModule = QPageModule.pageModule;
        Predicate predicate = qPageModule.type.eq("dingtalkISV");
        PageRequest pageRequest = new PageRequest(pageIndex,10);
        List<PageModule> pageModules=
        queryFactory.select(qPageModule)
                .from(qPageModule)
                .where(predicate)
                .offset(pageRequest.getOffset())
                .limit(pageRequest.getPageSize())
                .fetch()
                ;

        return pageModules;
    }

    public List<UserWithBlogs> getBlogList(int pageIndex){

        QUser qUser = QUser.user;
        Predicate predicate = qUser.nickname.like("s");
        PageRequest pageRequest = new PageRequest(pageIndex,10);
        List<UserWithBlogs> list= userRepository.getUserWithBlog(predicate,pageRequest);

        return list;

//        QUser user= QUser.user;
//        QBlog blog=QBlog.blog;
//        JPAQuery<User> qu= queryFactory.selectFrom(user);
//
//        JPAQuery<Tuple> jpaQuery = queryFactory.select(user, blog)
//                .from(user)
//                .innerJoin(blog)
//                .on(blog.user_id.intValue().eq(user.id.intValue()))
//                ;
//        List<Tuple> list1= jpaQuery.fetch();

// 基本查询
//        List<Blog> list=blogRepository.findAll();
//        return list;
    }
}
